package com.nanfang51g3.ckz.ui.login;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.forget.ForgetPassword1Activity;
import com.nanfang51g3.ckz.ui.home.HomeMainActivity;
import com.nanfang51g3.ckz.ui.registered.Registered1Activity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.widget.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2016/4/13.
 */
public class LoginActivity extends Base2Activity implements LoginContract.View {

    LoginContract.LoginActionsListener loginActionsListener;
    @Bind(R.id.iv_font)
    ImageView ivFont;
    @Bind(R.id.iv_info)
    ImageView ivInfo;
    @Bind(R.id.view_line_i)
    View viewLineI;
    @Bind(R.id.cet_login_name)
    ClearEditText etName;
    @Bind(R.id.relative_i)
    RelativeLayout relativeI;
    @Bind(R.id.iv_password)
    ImageView ivPassword;
    @Bind(R.id.view_line_ii)
    View viewLineIi;
    @Bind(R.id.cet_login_password)
    ClearEditText etPassword;
    @Bind(R.id.relative_ii)
    RelativeLayout relativeIi;
    @Bind(R.id.iv_login_select)
    ImageView ivSelect;
    @Bind(R.id.iv_login_none_select)
    ImageView ivNoneSelect;
    @Bind(R.id.iv_login)
    ImageView iv_login;
    @Bind(R.id.tv_keep_password)
    TextView tvKeepPassword;
    @Bind(R.id.tv_forget_password)
    TextView tvForgetPassword;
    @Bind(R.id.relative_iii)
    RelativeLayout relativeIii;
    @Bind(R.id.tv_login)
    TextView tvLogin;
    @Bind(R.id.tv_register)
    TextView tvRegister;

    boolean selectPasswordBoolean = false;

    @Override
    public IPresenter getPresenter() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.login;
    }

    @Override
    public boolean isShowLeftIcon() {
        return false;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_launcher;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_login;
    }

    @Override
    public void onClickLeft(View v) {

    }

    @Override
    public void onClickRight(View v) {

    }

    @Override
    public void showProgress() {
        super.showPro(R.string.str_progress_login);
    }

    @Override
    public void dismissProgress() {
        super.dismissPro();
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void startShowView() {
        startActivity(new Intent(this, HomeMainActivity.class));
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginActionsListener = DaggerLoginContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .loginPresenterModule(new LoginPresenterModule(this))
                .build().getLoginPresenter();
        KLog.i("TAG", loginActionsListener == null ? "loginActionsListener==null" : "loginActionsListener!=null");
        ButterKnife.bind(this);
        tvForgetPassword.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        String username = (String) SPUtil.get(this, "phone", "");
        String password = (String) SPUtil.get(this, "password", "");
        boolean isP = (Boolean) SPUtil.get(this, "is_p", false);
        KLog.i("Evd", isP + "-" + password);
        etName.setText(username);
        if (isP) {
            ivSelect.setVisibility(View.VISIBLE);
            etPassword.setText(password);
            ivNoneSelect.setVisibility(View.INVISIBLE);
            ivSelect.setVisibility(View.VISIBLE);
        } else {
            ivNoneSelect.setVisibility(View.VISIBLE);
        }

    }

    @OnClick(R.id.iv_login_select)
    public void iv_login_select(View view) {
        selectPasswordBoolean = true;
        SPUtil.putAndApply(this, "is_p", false);
        KLog.i("Evd", "iv_login_select");
        ivNoneSelect.setVisibility(View.VISIBLE);
        ivSelect.setVisibility(View.INVISIBLE);

    }

    @OnClick(R.id.iv_login_none_select)
    public void iv_login_none_select(View view) {
        KLog.i("Evd", "iv_login_none_select");
        selectPasswordBoolean = false;
        SPUtil.putAndApply(this, "is_p", true);
        ivNoneSelect.setVisibility(View.INVISIBLE);
        ivSelect.setVisibility(View.VISIBLE);
    }

    @OnClick(R.id.tv_forget_password)
    public void tv_forget_password(View view) {
        startActivity(new Intent(this, ForgetPassword1Activity.class));
    }

    @OnClick(R.id.tv_login)
    public void tv_login(View view) {
        loginActionsListener.https12306();
        String name = etName.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            EasyToast.showShort(this, "号码不能为空");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            EasyToast.showShort(this, "密码不能为空");
            return;
        }

        /**
         * @see LoginPresenter#login(String, String)
         */
//        loginActionsListener.login(name, password);

    }

    @OnClick(R.id.tv_register)
    public void tv_register(View view) {
        startActivity(new Intent(this, Registered1Activity.class));
    }

//    @OnTextChanged(value = R.id.et_name, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
//    void et_name(CharSequence text) {
//
//    }
//
//    @OnTextChanged(value = R.id.et_password, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
//    void et_password(CharSequence text) {
//
//
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }
}
